#include <iostream>
#include <cstring>
#include <stdio.h>
using namespace std;


char ss[20][100];
int len;
int  count[100]; 
char input[100],temp[100];


int findself(int n)
{
	int i;
	for (i=0; i<n; i++)
		if (strcmp(ss[i], ss[n])==0)
			return i;
	return -1;
}

int main()
{
	int i, j, k, step, flag, same;
	
	

	while (1)
	{
		memset(ss, 0, sizeof(ss));
		flag=0;
		cin >> ss[0];
		if (ss[0][0]=='-') break;
		for (step=1; step<=15; ++step)
		{
		
			memset(count, 0, sizeof(count));
			memset(input, 0, sizeof(input));

			len=strlen(ss[step-1]);
			for (i=0; i<len; ++i)
				count[ ss[step-1][i]-'0']++;

			for (i=0; i<=9; ++i)
				if (count[i]!=0)
				{
					sprintf(temp,"%d%d", count[i],i);
					strcat(input, temp);
				}
			/*/debug
			cout << input << endl;
			//debug */
			strcpy( ss[step], input);
			same=findself(step);
			if (same!=-1) 
			{
				if (same+1==step && step==1) flag=1;
				else if (same+1==step) flag=2;
				else 
				{
					flag=3;
					k=step-same;
				}
				break;
			}
		}

	
		if (flag==1 && step==1) cout << ss[0] << " is self-inventorying" << endl;
		else if (flag==3 && step<=15) cout << ss[0] << " enters an inventory loop of length " << k << endl;
		else if (flag==2 )	cout << ss[0] << " is self-inventorying after "<< step-1 <<" steps" << endl; 
		else cout << ss[0] << " can not be classified after 15 iterations" << endl;
		
		


	}

	return 0;
}